package com.shizhanzhe.qdd.forward.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.qdd.R;
import com.shizhanzhe.qdd.forward.adapter.AllManagerAdapter;
import com.shizhanzhe.qdd.forward.adapter.ManagerAdapter;
import com.shizhanzhe.qdd.forward.adapter.OnRecyclerItemClickListener;
import com.shizhanzhe.qdd.forward.bean.Class;
import com.shizhanzhe.qdd.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zz9527 on 2019/4/15.
 */
public class ClassManagerActivity extends Activity {
    @BindView(R.id.backarea)
    RelativeLayout backarea;
    @BindView(R.id.tv_my)
    TextView tvMy;
    @BindView(R.id.set)
    TextView set;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.recycler_add)
    RecyclerView recycleradd;
    private ManagerAdapter adapter;
    private AllManagerAdapter addadapter;
    private List<Class> list;
    private List<Class> add;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classmanager);
        ButterKnife.bind(this);
        ImmersionBar.with(this).statusBarColor(R.color.them).fitsSystemWindows(true).init();
        recycleradd.setLayoutManager(new GridLayoutManager(this, 3));
        addadapter = new AllManagerAdapter(this);
        recycleradd.setAdapter(addadapter);
        list = new ArrayList<>();
        add = new ArrayList<>();
        String json = (String) SharedPreferencesUtils.getParam(this, "my", "");
        if ("".equals(json)){
            list.add(new Class("推荐", "0", true));
            list.add(new Class("养生", "1", true));
            list.add(new Class("情感", "2", true));
            list.add(new Class("生活", "3", true));
            list.add(new Class("美食", "4", true));
            list.add(new Class("育儿", "5", true));
            list.add(new Class("搞笑", "6", true));
            list.add(new Class("时尚", "7", true));
            list.add(new Class("科技", "8", true));
            list.add(new Class("职场", "9", true));
            list.add(new Class("明星", "10", true));
            list.add(new Class("军事", "11", true));
            list.add(new Class("两性", "12", true));
            list.add(new Class("佛教", "13", true));
            list.add(new Class("美女", "14", true));
        }else {
            Gson gson = new Gson();
            List<Class> my = gson.fromJson(json, new TypeToken<List<Class>>() {
            }.getType());
            for (Class bean :
                    my) {
                if (!bean.isShow()){
                    add.add(bean);
                }else {
                    list.add(bean);
                }
            }
            addadapter.updateData(add);
        }


        recycler.setLayoutManager(new GridLayoutManager(this, 3));
        adapter = new ManagerAdapter(this);
        recycler.setAdapter(adapter);
        adapter.updateData(list);
        final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {

                if ("完成".equals(set.getText().toString())&&recyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |
                            ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                    final int swipeFlags = 0;
                    return makeMovementFlags(dragFlags, swipeFlags);
                }
                return 0;
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                if (!"完成".equals(set.getText().toString())){
                    return false;
                }
                //得到当拖拽的viewHolder的Position
                int fromPosition = viewHolder.getAdapterPosition();
                //拿到当前拖拽到的item的viewHolder
                int toPosition = target.getAdapterPosition();
                if (fromPosition < toPosition) {
                    for (int i = fromPosition; i < toPosition; i++) {
                        Collections.swap(list, i, i + 1);
                    }
                } else {
                    for (int i = fromPosition; i > toPosition; i--) {
                        Collections.swap(list, i, i - 1);
                    }
                }
                adapter.notifyItemMoved(fromPosition, toPosition);
                return true;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

            }

            /**
             * 重写拖拽可用
             * @return
             */
            @Override
            public boolean isLongPressDragEnabled() {
                return false;
            }

            /**
             * 长按选中Item的时候开始调用
             *
             * @param viewHolder
             * @param actionState
             */
            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
                    viewHolder.itemView.setBackgroundColor(Color.LTGRAY);
                }
                super.onSelectedChanged(viewHolder, actionState);
            }

            /**
             * 手指松开的时候还原
             * @param recyclerView
             * @param viewHolder
             */
            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                super.clearView(recyclerView, viewHolder);
                viewHolder.itemView.setBackgroundColor(0);
            }

        });
        recycler.addOnItemTouchListener(new OnRecyclerItemClickListener(recycler) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh) {

            }

            @Override
            public void onItemLongClick(RecyclerView.ViewHolder vh) {
                //判断被拖拽的是否是前两个，如果不是则执行拖拽
                if (vh.getLayoutPosition() != 0) {
                    itemTouchHelper.startDrag(vh);
                }
            }
        });
        itemTouchHelper.attachToRecyclerView(recycler);
        adapter.setOnItemClickListener(new ManagerAdapter.ItemClickListener() {
            @Override
            public void onItemDeleteClick(int position) {
                list.get(position).setShow(false);
                add.add(list.get(position));
                adapter.remove(position);
                addadapter.updateData(add);
            }
        });
        addadapter.setOnItemClickListener(new AllManagerAdapter.ItemClickListener() {
            @Override
            public void onItemAddClick(int position) {
                add.get(position).setShow(true);
                list.add(add.get(position));
                addadapter.remove(position);
                adapter.updateData(list);
            }
        });
    }

    @OnClick({R.id.back, R.id.set})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.set:
                if (set.getText().toString().equals("编辑")){
                    set.setText("完成");
                    adapter.show();
                    addadapter.show();
                }else if (set.getText().toString().equals("完成")){
                    set.setText("编辑");
                    adapter.hide();
                    addadapter.hide();
                    list.addAll(add);
                    Gson gson = new Gson();
                    String json = gson.toJson(list);
                    SharedPreferencesUtils.setParam(ClassManagerActivity.this,"my",json);
                    Intent intent = new Intent();
                    intent.putExtra("json",json);
                    setResult(1,intent);
                    finish();
                }
                break;
            default:
                break;
        }

    }
}
